/*-
 * <<
 * task
 * ==
 * Copyright (C) 2019 sia
 * ==
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * >>
 */

package com.sia.scheduler.web.controller;

import com.ive4.boot.core.json.Jsons;
import com.ive4.boot.core.utils.Checks;
import com.sia.scheduler.service.BasicJobService;
import com.sia.scheduler.util.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * WebJobController
 *
 * @see
 * @author maozhengwei
 * @date 2018-04-17 11:13
 * @version V1.0.0
 */
@RestController
@RequestMapping("/jobapi")
@Slf4j
public class WebJobController {

  @Autowired private BasicJobService basicJobService;

  /**
   * 前端调用接口 runOnce
   *
   * @param jobGroupName
   * @param jobKey
   * @return
   */
  @RequestMapping(
      value = "/runOnce/{jobGroupName}/{jobKey}",
      method = {RequestMethod.POST, RequestMethod.GET})
  @ResponseBody
  public String runOnce(@PathVariable String jobGroupName, @PathVariable String jobKey) {
    if (Checks.isEmpty(jobGroupName) || Checks.isEmpty(jobKey)) {
      return Jsons.write(Constants.FAIL);
    }
    try {
      boolean status = basicJobService.runOnce(jobGroupName, jobKey);
      return Jsons.write(status);
    } catch (SchedulerException e) {
      log.error(Constants.LOG_PREFIX + " runOnce jobs Exception : ", e);
    }
    return Jsons.write(Constants.FAIL);
  }
}
